package com.jlnk.androidfmc.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.jlnk.androidfmc.entity.Address;
import com.jlnk.androidfmc.room.AddressDB;

import java.util.ArrayList;
import java.util.List;

public class AddressDao {

  private final SQLiteDatabase db;

  public AddressDao(Context context) {
    AddressDB dbHelper = new AddressDB(context);
    db = dbHelper.getWritableDatabase();
  }

  @SuppressLint("Range")
  public List<Address> queryAll() {
    List<Address> addressList = new ArrayList<>();
    Cursor cursor = db.query("Address", null, null, null, null, null, null);

    if (cursor.moveToFirst()) {
      do {
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String phone = cursor.getString(cursor.getColumnIndex("phone"));
        String province = cursor.getString(cursor.getColumnIndex("province"));
        String city = cursor.getString(cursor.getColumnIndex("city"));
        String district = cursor.getString(cursor.getColumnIndex("district"));
        String addressDetail = cursor.getString(cursor.getColumnIndex("addressDetail"));
        int isDefault = cursor.getInt(cursor.getColumnIndex("isDefault"));

        Address address = new Address(id, name, phone, province, city, district, addressDetail,
            isDefault);
        addressList.add(address);
      } while (cursor.moveToNext());
    }

    cursor.close();
    return addressList;
  }

  public void insert(Address address) {
    if (address.getIsDefault() == 1) {
      clearAllIsDefault();
    }

    ContentValues values = new ContentValues();
    values.put("name", address.getName());
    values.put("phone", address.getPhone());
    values.put("province", address.getProvince());
    values.put("city", address.getCity());
    values.put("district", address.getDistrict());
    values.put("addressDetail", address.getAddressDetail());
    values.put("isDefault", address.getIsDefault());

    db.insert("address", null, values);
  }

  public void delById(int id) {
    db.delete("address", "id = ?", new String[]{String.valueOf(id)});
  }

  public void update(Address address, boolean isDefault) {
    if (isDefault) {
      clearAllIsDefault();
    }
    Log.d("Fleey", "address: " + address + "isDefault: " + isDefault);

    ContentValues values = new ContentValues();
    values.put("name", address.getName());
    values.put("phone", address.getPhone());
    values.put("province", address.getProvince());
    values.put("city", address.getCity());
    values.put("district", address.getDistrict());
    values.put("addressDetail", address.getAddressDetail());
    values.put("isDefault", isDefault ? 1 : 0);

    db.update("address", values, "id = ?", new String[]{String.valueOf(address.getId())});
  }

  private void clearAllIsDefault() {
    ContentValues values = new ContentValues();
    values.put("isDefault", 0);
    db.update("address", values, null, null);
  }
}